package com.jinbo.footmessage.order;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.jinbo.footmessage.BaseActivity;
import com.jinbo.footmessage.MyApplication;
import com.jinbo.footmessage.NavigationActivity;
import com.jinbo.footmessage.R;
import com.jinbo.footmessage.httpbean.OrderBean;
import com.jinbo.footmessage.httpbean.OrdersBean;
import com.jinbo.footmessage.httpbean.ProjectBean;
import com.jinbo.footmessage.httpretrofit.BusinessController;
import com.jinbo.footmessage.httpretrofit.Listener;
import com.jinbo.footmessage.serverInf.FragAdapter;
import com.jinbo.footmessage.serverInf.PickerTimeActivity;
import com.jinbo.footmessage.serverInf.ProjectItemListViewAdapter;
import com.jinbo.footmessage.serverInf.ServerInfActivity;
import com.jinbo.footmessage.serverInf.ShopItemListViewAdapter;
import com.jinbo.footmessage.utils.ActivitiesHelper;
import com.jinbo.footmessage.utils.LocalPreferencesHelper;
import com.jinbo.footmessage.view.TitleBarView;

import java.util.ArrayList;
import java.util.List;


public class OrderActivity extends BaseActivity
        implements View.OnClickListener {

  //  List<OrderBean> orders=new ArrayList<>();
    public static final int REQUEST_DETAIL = 101;
    private Callbacks mCallBacks1,mCallBacks2;

    public interface Callbacks {
        public void change(List<OrderBean> orders);
    }

    private View mLoadView;

    private ImageView mLeftLineIv, mRightLineIv;
    private Button mLeftBt, mRightBt;
    private ViewPager vp;

    //private PullRefreshLayout mPullRefreshLayout;

    public static void launch(Activity activity) {
        Intent intent = new Intent(activity, OrderActivity.class);
        activity.startActivity(intent);
    }



    int flag;
    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        TitleBarView titleBarView = (TitleBarView) findViewById(R.id.titleBar);
        titleBarView.setCenterTitle("订单");
        titleBarView.setLeftBackButtonIsVisibler(true);
        titleBarView.setListener(TitleBarView.TitleBarEnum.ll_leftBackImageLayout, this);
        titleBarView.setListener(TitleBarView.TitleBarEnum.ll_leftBackImageLayout, this);
        titleBarView.setRightTextIsVisible(true, "刷新");
        titleBarView.setListener(TitleBarView.TitleBarEnum.ll_rightNextTextLayout,this);
        Intent intent = getIntent();
        flag= intent.getIntExtra("flag", -1);
        mLoadView = findViewById(R.id.loading);
        /*mPullRefreshLayout = (PullRefreshLayout) findViewById(R.id.mainRefreshLayout);//下拉刷新，第三方控件

        mPullRefreshLayout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {


                updateData();
            }
        });
*/
        updateData();

        mLeftLineIv = (ImageView) findViewById(R.id.left_line);
        mRightLineIv = (ImageView) findViewById(R.id.right_line);



        mLeftBt = (Button) findViewById(R.id.left);
        mRightBt = (Button) findViewById(R.id.right);
        mLeftBt.setOnClickListener(this);
        mRightBt.setOnClickListener(this);

        List<Fragment> fragments = new ArrayList<Fragment>();
        NotConfirmFragment notConfirmFragment= new NotConfirmFragment();
        //Bundle arg = new Bundle();
        //arg.putInt("number",number);
        //NotConfirmFragment.setArguments(arg);
        //mCallBacks = (Callbacks) NotConfirmFragment;
        ConfirmedFragment confirmedFragment = new ConfirmedFragment();
        //ConfirmedFragment.setArguments(arg);
        //mCallBacks2 = (Callbacks2) ConfirmedFragment;
        fragments.add(notConfirmFragment);
        fragments.add(confirmedFragment);


        FragAdapter adapter = new FragAdapter(getSupportFragmentManager(), fragments);
        vp = (ViewPager) findViewById(R.id.viewpager);
        vp.setOffscreenPageLimit(2);
        vp.setAdapter(adapter);
        //preferences=getSharedPreferences("pager",MODE_PRIVATE);
        // args=preferences.getInt("pager", 0);
        vp.setCurrentItem(0);

        vp.setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageSelected(int arg0) {

                if (arg0 == 0) {
                    mLeftLineIv.setVisibility(View.VISIBLE);
                    mRightLineIv.setVisibility(View.INVISIBLE);

                } else if (arg0 == 1) {
                    mLeftLineIv.setVisibility(View.INVISIBLE);
                    mRightLineIv.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });

        mCallBacks1 = (Callbacks) notConfirmFragment;
        mCallBacks2 = (Callbacks) confirmedFragment;

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.left_back_layout:
                if(flag!=1)
                  clearActivity();//Except(NavigationActivity.class);
                else
                  finish();
                break;
            case R.id.left:
                vp.setCurrentItem(0);
                mLeftLineIv.setVisibility(View.VISIBLE);
                mRightLineIv.setVisibility(View.INVISIBLE);
                break;
            case R.id.right:
                vp.setCurrentItem(1);
                mLeftLineIv.setVisibility(View.INVISIBLE);
                mRightLineIv.setVisibility(View.VISIBLE);
                break;
            case R.id.right_next_text_layout:
                updateData();
                break;


        }

    }
    public void onBackPressed() {
        if(flag!=1)
            clearActivity();//Except(NavigationActivity.class);
        else
            finish();
    }


    void clearActivity()
    {
        NavigationActivity.launch(this);
        finish();
        ActivitiesHelper.getInstance().closeAll();
    }


    void updateData()
    {
        String user_id= MyApplication.local.getString(LocalPreferencesHelper.USER_ID);
        BusinessController.getInstance().NotConfirmOrder(user_id, new Listener<OrdersBean>() {
            @Override
            public void onStart(Object... params) {
                //showProgressDialog("请稍后...");
                //mLoadView.setVisibility(View.VISIBLE);
                //mPullRefreshLayout.setRefreshing(true);
                mLoadView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onComplete(OrdersBean result, Object... params) {
                //dismissProgressDialog();
                // mPullRefreshLayout.setRefreshing(false);
                mLoadView.setVisibility(View.GONE);
                mCallBacks1.change(result.orders);
                // mCallBacks2.change(result.orders);


            }

            @Override
            public void onFail(final String msg, Object... params) {
                mLoadView.setVisibility(View.VISIBLE);
                //dismissProgressDialog();
                //mLoadView.setVisibility(View.GONE);
                // mPullRefreshLayout.setRefreshing(false);
                if (TextUtils.isEmpty(msg)) {
                } else {
                    show(msg);
                }
            }
        });

        BusinessController.getInstance().confirmOrder(user_id, 0, 10, new Listener<OrdersBean>() {
            @Override
            public void onStart(Object... params) {
                //showProgressDialog("请稍后...");
                //mLoadView.setVisibility(View.VISIBLE);
                //mPullRefreshLayout.setRefreshing(true);
                mLoadView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onComplete(OrdersBean result, Object... params) {
                //dismissProgressDialog();
                // mPullRefreshLayout.setRefreshing(false);
                mLoadView.setVisibility(View.GONE);
                mCallBacks2.change(result.orders);
                // OrderActivity.this.
                //orders = result.orders;
                // mCallBacks2.change(result.orders);


            }

            @Override
            public void onFail(final String msg, Object... params) {
                mLoadView.setVisibility(View.VISIBLE);
                //dismissProgressDialog();
                //mLoadView.setVisibility(View.GONE);
                // mPullRefreshLayout.setRefreshing(false);
                if (TextUtils.isEmpty(msg)) {
                } else {
                    show(msg);
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_DETAIL) {
                updateData();
            }
        }
    }
}









		